package com.hfjyz.tutor.tutorcall.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.hfjyz.base.action.GridBaseAction;
import com.hfjyz.base.bean.SearchCondition;
import com.hfjyz.base.constant.BaseConstant;
import com.hfjyz.base.exception.BaseException;
import com.hfjyz.tutor.tutor.bean.TutorBean;
import com.hfjyz.tutor.tutorcall.bean.TutorCallBean;
import com.hfjyz.tutor.tutorcall.bean.TutorCallSearchBean;
import com.hfjyz.tutor.tutorcall.bean.TutorCallSearchCondition;
import com.hfjyz.tutor.tutorcall.service.TutorCallService;

public class TutorCallAction extends GridBaseAction {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -329879627858090900L;
    private static final Log SYS_LOG = LogFactory.getLog(BaseConstant.SYS_LOG);
    private TutorCallService service = null;
    private TutorCallBean tutorCall = null;
    private String selectIds = null;
    private Integer tutorId = null;
    protected TutorCallSearchBean tutorCallSearch = null;

    public TutorCallService getService() {
        return service;
    }

    public void setService(TutorCallService service) {
        this.service = service;
    }

    public TutorCallBean getTutorCall() {
        return tutorCall;
    }

    public void setTutorCall(TutorCallBean tutorCall) {
        this.tutorCall = tutorCall;
    }

    public String getSelectIds() {
        return selectIds;
    }

    public void setSelectIds(String selectIds) {
        this.selectIds = selectIds;
    }

    public Integer getTutorId() {
        return tutorId;
    }

    public void setTutorId(Integer tutorId) {
        this.tutorId = tutorId;
    }

    public TutorCallSearchBean getTutorCallSearch() {
        return tutorCallSearch;
    }

    public void setTutorCallSearch(TutorCallSearchBean tutorCallSearch) {
        this.tutorCallSearch = tutorCallSearch;
    }

    @Override
    protected List<TutorCallBean> getBeanList() throws BaseException {
        HttpSession session = ServletActionContext.getRequest().getSession();
        session.setAttribute(BaseConstant.SESSION_CONDITION_KEY, tutorCallSearch);

        return service.findTutorCall(getSearchCondition(), tutorCallSearch);
    }

    @Override
    protected long getTotalCount() throws BaseException {
        return service.getTutorCallCount(tutorCallSearch);
    }

    public String save() throws BaseException {
        map = new HashMap<String, Object>();
        try {
            service.saveTutorCall(tutorCall);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String delete() throws BaseException {
        map = new HashMap<String, Object>();
        try {
            if (selectIds == null) {
                SYS_LOG.error("未选择待删除的老师调用！");
                throw new BaseException("未选择待删除的老师调用！");
            }
            String[] idArray = selectIds.split(",");
            List<Integer> idList = new ArrayList<Integer>();
            for (String id : idArray) {
                idList.add(Integer.valueOf(id));
            }
            service.deleteTutorCall(idList);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String updateInit() throws BaseException {
        map = new HashMap<String, Object>();
        try {
            Integer id = Integer.valueOf(selectIds);
            tutorCall = service.updateInitTutorCall(id);
            List<TutorBean> tutorList = service.findAllTutor();

            map.put("tutorCall", tutorCall);
            map.put("tutorList", tutorList);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String listTutor() throws BaseException {
        map = new HashMap<String, Object>();
        try {
            List<TutorBean> tutorList = service.findAllTutor();
            map.put("tutorList", tutorList);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public SearchCondition getSearchCondition() {
        SearchCondition cond = new TutorCallSearchCondition();
        cond.setPageNum(pageNum);
        cond.setRowCountPerPage(rowCountPerPage);
        cond.setSortColumn(sortColumn);
        cond.setSortDirection(sortDirection);

        return cond;
    }
}
